package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import library.and.xps.com.step.R;
import library.and.xps.com.step.SPConstant;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.TeamActiveMemberAdapter;
import library.and.xps.com.step.m.model.ActiveDetailIntentParams;
import library.and.xps.com.step.m.model.ActiveMemberRanking;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.DateUtil;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.view.base.BaseActivity;

public class TeamActiveDetailActivity extends BaseActivity {
    ImageView iv_back_header, iv_front_header, iv_back;
    TextView tv_active_time, tv_active_membercount, tv_active_titel, tv_state;
    ListView lv_members;
    TeamActiveMemberAdapter teamActiveMemberAdapter;
    CardView card_state;
    ActiveDetailIntentParams activeDetailIntentParams;

    @Override
    protected int getLayout() {
        return R.layout.activity_team_active_detail;
    }

    @Override
    protected void setData() {
        Intent i = getIntent();
        ActiveDetailIntentParams activeDetailIntentParams = (ActiveDetailIntentParams) i.getSerializableExtra("active");
        if (activeDetailIntentParams == null) return;

        teamActiveMemberAdapter = new TeamActiveMemberAdapter(this);
        lv_members.setAdapter(teamActiveMemberAdapter);
        initData(activeDetailIntentParams);
    }

    void initData(ActiveDetailIntentParams activeDetailIntentParams) {
        Logger.d(activeDetailIntentParams.toString());
        this.activeDetailIntentParams = activeDetailIntentParams;
        initActiveState(activeDetailIntentParams.getState());
        initMemberFormService(activeDetailIntentParams.getActivityid() + "");
        tv_active_membercount.setText("共" + activeDetailIntentParams.getPeople() + "人");
        tv_active_time.setText(DateUtil.retrofitDateString(activeDetailIntentParams.getStarttime()) + "-" + "\n" + DateUtil.retrofitDateString(activeDetailIntentParams.getEndtime()) + "");
        tv_active_titel.setText(activeDetailIntentParams.getTaname() + "");
        teamActiveMemberAdapter.setType(Integer.valueOf(activeDetailIntentParams.getType()));

        setUpImg(URLConstants.FILE.FileDownload + activeDetailIntentParams.getMark());
    }

    void initActiveState(int state) {
        switch (state) {
            case -1:
                card_state.setCardBackgroundColor(Color.parseColor("#60000000"));
                tv_state.setText("已结束");
                break;
            case 0:
                card_state.setCardBackgroundColor(Color.parseColor("#00B2EE"));
                tv_state.setText("即将开始");
                break;
            case 1:
                card_state.setCardBackgroundColor(Color.parseColor("#ef5757"));
                tv_state.setText("进行中");
                break;
        }
    }

    void initMemberFormService(String id) {
        String data = URLConstants.TEAMACTIVE.ACTIVITY_RESULT + "?activityid=" + id;
        OkHttpClientUtils.getDataAsync(this, data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                dissmisDialog();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                dissmisDialog();
                if (HttpResponsHelper.SuccessResponce(response)) {
                    ActiveMemberRanking activeMemberRanking = new Gson().fromJson(response.getBody(), ActiveMemberRanking.class);
                    teamActiveMemberAdapter.addData(activeMemberRanking.getList(), activeDetailIntentParams.getExercise());
                }
            }

            @Override
            public void onStart() {
                showDialogNoCancel();
            }
        }, "");
    }

    void setUpImg(String active_cover) {
        GlideUrl glideUrl = new GlideUrl(String.valueOf(active_cover), new LazyHeaders.Builder()
                .addHeader("token", JUtils.getSharedPreference().getString(SPConstant.USER_TOKEN, ""))
                .build());

        Glide.with(this).load(glideUrl)
                .placeholder(R.drawable.glide_place_holder)
                .bitmapTransform(new BlurTransformation(this, 30, 1))
                .into(iv_back_header);

        Glide.with(this).load(glideUrl)
                .placeholder(R.drawable.glide_place_holder)
                .bitmapTransform(new RoundedCornersTransformation(this, 25, -10, RoundedCornersTransformation.CornerType.ALL))
                .into(iv_front_header);
    }

    @Override
    protected void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        iv_back_header = r2v(R.id.iv_back_header);
        iv_front_header = r2v(R.id.iv_front_header);
        tv_active_time = r2v(R.id.tv_active_time);
        iv_back = r2v(R.id.iv_back);
        card_state = r2v(R.id.card_state);
        tv_state = r2v(R.id.tv_cancel_pk);
        iv_back.setOnClickListener(this);
        tv_active_membercount = r2v(R.id.tv_active_membercount);
        lv_members = r2v(R.id.lv_members);
        tv_active_titel = r2v(R.id.tv_active_titel);
        tv_active_time.setOnClickListener(this);
        tv_active_membercount.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_active_time:

                break;
//            case R.id.tv_active_member_count:
//
//                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
